home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Inside Mac Games Volume 3 #12
/
IMG 33 Dec 1995.iso
/
Important Files
/
MGW1Codeƒ
/
MGWUtilities1.c
< prev
Wrap
Text File
|
1995-06-29
|
15KB
|
352 lines
//==============================================================================================\\
// ----------------------------------------------------------------------------------- \\
// MGWUtilities1.c version 1.0.0 copyright © 1993…1995 Jamie McCornack, john calhoun \\
// ----------------------------------------------------------------------------------- \\
// Alerts and other utilities for Macintosh GameWriter 1.0.0, a training program… \\
// …for beginning Mac game programmers. MGW1 includes MGWExterns1.h, MGWUtilities1.c,… \\
// …MGWSound1.c, MGWGraphics1.c, MGWGraphicsBWLite1.c, HelloWorld.rsrc and an assortment… \\
// …of demo programs; projects HelloWorld1.π etc. and source code files HelloWorld1.c etc. \\
// A tutorial is available in Tricks of the Mac Game Programming Gurus, published… \\
// …by Hayden Books, August 1995. \\
// \\
// This code is offered by the copyright holders for no fee and for whatever use… \\
// …you care to make of it, but we do hope you remember where it came from. \\
// \\
// Please send bug reports to MacGameDev at America OnLine. macgamedev@aol.com \\
// Suggestions and observations are also appreciated. \\
// Updates and upgrades will be available now and then from the above e-mail address. \\
//==============================================================================================\\
// This unit checks and verifies the environment (which model Mac running which version System)…
// …and handles Alerts. It uses 'STR#' resources for alert messages, or your own strings.
// IMPORTANT NOTE: Thanks to a bit of late-breaking knowledge, this code departs slightly from…
// …the text of Chapter 0 of Tricks of the Mac Game Programming Gurus. It does not use the variable…
// …thisMac of variable type tMacEnvironment to store start-up information about a paticular Mac's…
// …hardware and system. Since generally, your program will only look at this data once, it's not…
// …worth hanging on to for the whole game.
#include "MGWExterns1.h"
#include <palettes.h>
GDHandle gThisGDevice; // Just this one variable.
//------------------------------------------------------------ Prototypes
Boolean MacHasColorQD(void);
Boolean MacIsAtLeastAMacII(void);
Boolean MacHasSystem7(void);
Boolean MacHasSystem605(void);
Boolean MacHasSystem602(void);
Boolean MacIsAtLeastMacII (void);
Boolean CanWeDisplay8Bit (GDHandle theDevice);
void SwitchToDepth (short newDepth, Boolean doColor);
void CheckMacEnviro (void);
void InitToolbox(void);
void FindOurDevice (void);
//============================================================== Functions
//-------------------------------------------------------------- CenterAlert
// Handy function to center any alert within the main monitor. Not dead center, the upper…
// …center, where Mac users (and Apple human interface guidelines) expect them.
// For System 6.x use, since System 7 can center alerts automatically (via ResEdit).
void CenterAlert (short theAlertID)
{
AlertTHndl alertHandle;
Rect theScreen, alertRect;
short horiOff, vertOff; // Distance in pixels to offset the alert.
Byte wasState;
theScreen = qd.screenBits.bounds; // Get main monitor's bounds.
theScreen.top += LMGetMBarHeight(); // Account for menubar height.
// Get handle to alert resource.
alertHandle = (AlertTHndl)GetResource('ALRT', theAlertID);
if (alertHandle != 0L) // Make sure we got it!
{ // Remember its "state" (locked, etc.)
wasState = HGetState((Handle)alertHandle);
HLock((Handle)alertHandle); // Lock it so Memory Mnager can't move it.
// Get a copy of its bounds and zero.
alertRect = (**alertHandle).boundsRect;
OffsetRect(&alertRect, -alertRect.left, -alertRect.top);
// Calculate offsets for centering bounds.
horiOff = ((theScreen.right - theScreen.left) - alertRect.right) / 2;
vertOff = ((theScreen.bottom - theScreen.top) - alertRect.bottom) / 3;
// And offset the bounds copy.
OffsetRect(&alertRect, horiOff, vertOff + LMGetMBarHeight());
// Set alerts bounds to our centered rect.
(**alertHandle).boundsRect = alertRect;
HSetState((Handle)alertHandle, wasState); // Set it to its original state.
}
}
//-------------------------------------------------------------- RedAlert
// Generic error function. This is called when there is no hope of recovering from the error.
// A simple alert is brought up and the text passed in the whatGives field of the 'STR#'…
// rRedAlertStringIDs is displayed. When the user clicks the Okay button, we quit to the Finder.
void RedAlert (short whatGives)
{
short doWhat;
Str255 errorString, errorNumberString;
GetIndString(errorString, rRedAlertStringIDs, whatGives);
// Set errorString to string number whatGives in 'STR#' resource rRedAlertStringIDs.
NumToString(whatGives, errorNumberString);
// Set errorNumberString to decimal digits representing whatGives. Useful if you (the…
// …programmer) get an unexpected alert message. Check it against the constants and 'STR#'.
ParamText(errorString, "\p", "\p", "\p"); // Replace ^0 in alert with error mssg.
// CenterAlert(rRedAlertID); // Not needed with System 7.x. Use for System 6.x.
InitCursor(); // Show arrow cursor.
doWhat = Alert(rRedAlertID, 0L); // Bring up alert.
ExitToShell(); // Quit to Finder.
}
//-------------------------------------------------------------- YellowAlert
// This error function is called when the player has an option between quitting or continuing.
// A simple alert is brought up and the text passed in the whatGives field of the 'STR#'…
// rYellowAlertStringIDs is displayed. When the user clicks the Finder button, we quit…
// …to the Finder, when the user clicks the Play button, we continue to play.
void YellowAlert (short whatGives)
{
short doWhat;
Str255 errorString, errorNumberString;
GetIndString(errorString, rYellowAlertStringIDs, whatGives);
// Set errorString to string number whatGives in 'STR#' resource rRedAlertStringIDs.
NumToString(whatGives, errorNumberString);
// Set errorNumberString to decimal digits representing whatGives. Useful if you (the…
// …programmer) get an unexpected alert message. Check it against the constants and 'STR#'.
ParamText(errorString, errorNumberString, "\p", "\p"); // Replace ^0 in alert with error mssg.
// CenterAlert(rRedAlertID); // Not needed with System 7.x.
ShowCursor(); // Make sure the cursor is showing.
doWhat = Alert(rYellowAlertID, 0L); // Bring up alert, find out which item was chosen.
if (doWhat == kYellowExitItem) // This item# is the signal to exit the game.
ExitToShell(); // Quit to Finder.
HideCursor(); // IMPORTANT NOTE: If you want the cursor to show when YellowAlert…
} // …is finished, the program's next call should be ShowCursor().
//-------------------------------------------------------------- RedAlertString
// Generic error function. This is called when there is no hope of recovering from the error.
// A simple alert is brought up and the text passed in the StringPtr of some text imbedded…
// …in the program is displayed. When the user clicks the Okay button, we quit to the Finder.
void RedAlertString (StringPtr theString)
{
short doWhat;
ParamText(theString, "\p", "\p", "\p"); // Replace ^0 in alert with error mssg.
// CenterAlert(rRedAlertID); // Not needed with System 7.x.
InitCursor(); // Show arrow cursor.
doWhat = Alert(rRedAlertID, 0L); // Bring up alert.
ExitToShell(); // Quit to Finder.
}
//-------------------------------------------------------------- YellowAlertString
// This error function is called when the player has an option between quitting or continuing.
// A simple alert is brought up and the text passed in the StringPtr of some text imbedded…
// …in the program is displayed. When the user clicks the Finder button, we quit…
// …to the Finder, when the user clicks the Play button, we continue to play.
void YellowAlertString (StringPtr theString)
{
short doWhat;
ParamText(theString, "\p", "\p", "\p"); // Replace ^0 in alert with error mssg.
// CenterAlert(rRedAlertID); // Not needed with System 7.x.
ShowCursor(); // Make sure the cursor is showing.
doWhat = Alert(rYellowAlertID, 0L); // Bring up alert, find out which item was chosen.
if (doWhat == kYellowExitItem) // This item# is the signal to exit the game.
ExitToShell(); // Quit to Finder.
HideCursor(); // IMPORTANT NOTE: If you want the cursor to show when YellowAlertString…
} // …is finished, the program's next call should be ShowCursor().
//-------------------------------------------------------------- MacHasColorQD
// Simple function that returns TRUE if we're running on a Mac that…
// is running Color Quickdraw.
Boolean MacHasColorQD (void)
{
SysEnvRec thisWorld;
SysEnvirons(2, &thisWorld); // Call SysEnvirons() version 2.
return (thisWorld.hasColorQD); // Return whether it has Color QuickDraw.
}
//-------------------------------------------------------------- MacHasSystem7
// Returns TRUE if the Mac we're running on has System 7.0.0 or more recent.
// System 7 introduced…man, there are whole books on the subject.
// If your game is running routines that weren't introduced until System 7.x, call this first.
// You might also want to demand System 7 to save yourself hassles with compatability testing,…
// …but many older Macs were never upgraded to System 7 because System 7 is a memory hog,…
// …and many of those older Macs are in the kid's playroom now. I know many programmers feel…
// …old Macs and System 6.x are ready for the dumpster and the cutting edge is where it's at,…
// …but to paraphrase what Chinese mothers tell their kids when they won't eat their fried rice,…
// …there are children in Appalachia who would loooove to have your Mac II with System 6.0.8.
Boolean MacHasSystem7 (void)
{
SysEnvRec thisWorld;
Boolean haveIt;
SysEnvirons(2, &thisWorld); // Call the old SysEnvirons() function.
if (thisWorld.systemVersion >= 0x0700)
haveIt = TRUE; // Check the System version for 6.0.2…
else // or more recent
haveIt = FALSE;
return (haveIt);
}
//-------------------------------------------------------------- MacHasSystem605
// Returns TRUE if the Mac we're running on has System 6.0.5 or more recent.
// System 6.0.5 introduced the ability to switch color depths.
Boolean MacHasSystem605 (void)
{
SysEnvRec thisWorld;
Boolean haveIt;
SysEnvirons(2, &thisWorld); // Call the old SysEnvirons() function.
if (thisWorld.systemVersion >= 0x0605)
haveIt = TRUE; // Check the System version for 6.0.5…
else // or more recent
haveIt = FALSE;
return (haveIt);
}
//-------------------------------------------------------------- MacHasSystem602
// Returns TRUE if the Mac we're running on has System 6.0.2 or more recent.
// System 6.0.2 introduced lots of good sound routines. This may be all you need.
Boolean MacHasSystem602 (void)
{
SysEnvRec thisWorld;
Boolean haveIt;
SysEnvirons(2, &thisWorld); // Call the old SysEnvirons() function.
if (thisWorld.systemVersion >= 0x0602)
haveIt = TRUE; // Check the System version for 6.0.2…
else // or more recent
haveIt = FALSE;
return (haveIt);
}
//-------------------------------------------------------------- MacIsAtLeastMacII
// Returns TRUE if the Mac we're running is a Mac II or more recent.
// You may need more (or less), in which case this routine is easy to modify.
Boolean MacIsAtLeastMacII (void)
{
SysEnvRec thisWorld;
Boolean haveIt;
SysEnvirons(2, &thisWorld); // Call the old SysEnvirons() function.
if (thisWorld.machineType >= envMacII)
haveIt = TRUE; // Check the System version for 6.0.2…
else // or more recent
haveIt = FALSE;
return (haveIt);
}
//-------------------------------------------------------------- FindOurDevice
// Get a handle to the MainDevice (monitor with the Menubar).
void FindOurDevice (void)
{
gThisGDevice = GetMainDevice();
if (gThisGDevice == 0L) // If a nil handle is returned...
RedAlertString("\pCouldn't Find Our Device"); // call our universal error alert.
}
//-------------------------------------------------------------- WhatsOurDepth
// Function returns the current bit depth. For example, 4 = 16 colors, 8 = 256…
// colors.This function assumes System 6.0.5 or more recent and Color Quickdraw capable,…
// …so you should haved checked before attempting this call (see routines above).
short WhatsOurDepth (void)
{
short thisDepth;
char wasState;
gThisGDevice = GetMainDevice();
if (gThisGDevice != 0L) // Make sure we have device handle.
{
wasState = HGetState((Handle)gThisGDevice); // Remember the handle's state.
HLock((Handle)gThisGDevice); // Lock the device handle down.
// Get it's depth (pixelSize).
thisDepth = (**(**gThisGDevice).gdPMap).pixelSize;
HSetState((Handle)gThisGDevice, wasState); // Restore handle's state.
}
else
RedAlertString("\pUnknown Error."); // Post generic error message.
return (thisDepth); // Return screen depth.
}
//-------------------------------------------------------------- CheckMacEnviro
// This is the "wrapper" function that calls the above functions.
// After calling ToolBoxInit(), the program will call this function to see…
// …if the current Mac we're running on is capable of running this program.
void CheckMacEnviro (void)
{
if (!MacHasColorQD()) // If the game is in color, it needs Color QuickDraw.
RedAlert(kErrNoColor);
if (!MacHasSystem7()) // Actually, System 6.0.5 would run this demo. If an…
RedAlert(kErrOldSystem); // …earlier system will do for you, change this.
if (!MacIsAtLeastMacII()) // Your game may need more or less hardware.
RedAlert(kErrOldMacintosh); // Change this to suit your needs.
}
//-------------------------------------------------------------- InitToolbox
// A generic first function
//
// The calls herein MUST be called before you do anything else.
// Otherwise, you'll get all sorts of System Errors.
void InitToolbox (void)
{
InitGraf(&qd.thePort); // Initialize QuickDraw variables for our program.
InitFonts(); // Initialize fonts.
FlushEvents(everyEvent, 0); // Clear event queue of any pending events.
InitWindows(); // Initialize the Window Manager.
InitMenus(); // Ditto for the Menu Manager.
TEInit(); // blah, blah Text Edit.
InitDialogs(0L); // blah, blah Dialog Manager.
InitCursor(); // Set the cursor to the arrow cursor and init.
MaxApplZone(); // Grab application memory.
MoreMasters(); // Allocate a block of master pointers.
MoreMasters(); // And allocate more.
MoreMasters(); // And more.
MoreMasters(); // Hey, lets do it again too.
GetDateTime((unsigned long *)&qd.randSeed); // Randomize random seed.
}
//------------------------------------------------------------------------------------------\\
// End MGWUtilities1.c \\
//------------------------------------------------------------------------------------------\\